package Hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.soap.Name;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*map+过滤*/
@WebServlet("/namefilter")
public class searchfilter extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("进入检索方法");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        System.out.println(req.getParameter("search1"));
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "niit01");

        HTable table = new HTable(conf,"stu");
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        System.out.println("====="+scanner);
        List stulist = new ArrayList();//list创建
        for (Result result:scanner){
            Map<String,String> map = new HashMap<>();//提供数据一对一处理，将一行信息映射
            for (KeyValue kv:result.raw()){

                map.put(new String(kv.getQualifier()),new String(kv.getValue()));

            }
            if(map.get("Name").equals(req.getParameter("search1"))||map.get("Semester").equals(req.getParameter("search1"))||map.get("Student Status").equals(req.getParameter("search1")))
            {
                stu st = new stu();
                    System.out.println(map);
                    //System.out.println(req.getParameter("search1"));
                    st.setStudentID(map.get("Student ID"));//null
                    st.setName(map.get("Name"));
                    st.setDateOfenrollment(map.get("Date of Enrollment"));//null
                    st.setContactnumber(map.get("Contact Number"));//null
                    st.setStudentstatus(map.get("Student Status"));
                    st.setSemester(map.get("Semester"));

                stulist.add(st);//下一行
            }


        }
        System.out.println(stulist);
        req.setAttribute("stulist",stulist);
        session.setAttribute("stulist",stulist);//封装
        session.setAttribute("stulistsize",stulist.size());
        req.getRequestDispatcher("searchjs.jsp").forward(req,resp);


    }
}